import NProgress from 'nprogress'

import 'nprogress/nprogress.css'

// 创建一个NProgress实例的变量，初始值为null
let nProgressInstance: null | typeof NProgress = null

/**
 * 动态加载NProgress库，并进行配置。
 */
const loadNprogress = async (): Promise<typeof NProgress> => {
  if (nProgressInstance) return nProgressInstance
  nProgressInstance = NProgress
  nProgressInstance.configure({
    showSpinner: true,
    speed: 300
  })
  return nProgressInstance
}

/**
 * 开始显示进度条。
 */
export const startProgress = async (): Promise<void> => {
  const nprogress = await loadNprogress()
  nprogress?.start()
}

/**
 * 停止显示进度条，并隐藏进度条。
 */
export const stopProgress = async (): Promise<void> => {
  const nprogress = await loadNprogress()
  nprogress?.done()
}
